<?php
/**
 * CreateTime: 2023/12/1 16:25
 * User:LinQ
 */

namespace App\State;


class StatusState
{
    // 正常
    const OK_STATUS = 1;
    // 删除
    const DELETE_STATUS = 2;

    public function getStateDescription($status): string
    {
        return match ($status){
            self::OK_STATUS => trans('key.normal'),
            self::DELETE_STATUS => trans('key.delete'),
            default => "",
        };
    }

    public function stateChange(int $id, int $status, int $toStatus,$model,$key): bool
    {
        $statusStr = $this->getStateDescription($status);
        if(!$statusStr){
            s_fail_return(null,trans('message.start_status_err',[
                'name' => ""
            ]));
        }
        $toStatusStr = $this->getStateDescription($toStatus);
        if(!$toStatusStr){
            s_fail_return(null,trans('message.change_status_err',[
                'name' => ""
            ]));
        }
        try {
            $model->where($key,$id)
                ->where('status',$status)
                ->update([
                    'status' => $toStatus
                ]);
            return true;
        }catch (\Exception $exception){
            s_fail_return(null,'状态修改异常');
            return false;
        }
    }
}
